package com.log.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.log.common.vo.ResStatus;
import com.log.common.vo.ResultVo;
import com.log.dao.*;
import com.log.pojo.*;
import com.log.pojoVo.LogDistachedOrderVO;
import com.log.pojoVo.LogGoodsOutDetailVO;
import com.log.service.LogOutService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @auhthor xb
 * @create 2023/10/16
 **/
@Service
public class LogOutServiceImpl implements LogOutService {
    /*
     展示出库商品
     */
    @Resource
    LogGoodsOutInMapper logGoodsOutMapper;
    @Resource
    LogEmployeerMapper logEmployeerMapper;
    @Resource
    LogCarMapper logCarMapper;
    @Resource
    LogTransMapper logTransMapper;
    @Resource
    LogDepotMapper logDepotMapper;

/*
 司机运输模块
 传入2个参数，一个司机名字，一个出库单号,一个运输地址
 */
    @Override
    public ResultVo dispatchGoods(String name,int logCarId,int logGoodsOutId) {
        Example example=new Example(LogEmployeer.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("logEmployeeJob","司机");
        criteria.andEqualTo("logEmployeerState",0);
        criteria.andEqualTo("logEmployeeName",name);
        List<LogEmployeer> CarEmployers=logEmployeerMapper.selectByExample(example);
        //根据姓名查找空闲司机不存在，返回错误信息
        if(CarEmployers.size()==0) return new ResultVo(ResStatus.NO,"该司机暂时不能运输",CarEmployers);
        /*
        查找对应车辆的状态,如果空闲返回可用否则范围车辆不可用的信息
         */
        Example carexample=new Example(LogCar.class);
        Example.Criteria carcriteria=carexample.createCriteria();
        carcriteria.andEqualTo("logCarId",logCarId);
        //状态1-->可以使用
        carcriteria.andEqualTo("logCarStatus",1);
        List<LogCar> logCarList=logCarMapper.selectByExample(carexample);
        if(logCarList.size()==0) return new ResultVo(ResStatus.NO,"该车辆暂时不可用",null);

     //其他状态，司机和选择的车辆均可以使用

        //该司机状态空闲情况，修改司机状态
        if(CarEmployers!=null&&CarEmployers.size()>0) {
            LogEmployeer Employee = CarEmployers.get(0);
            Employee.setLogEmployeerState(1);
            logEmployeerMapper.updateByPrimaryKey(Employee);
        }
        /*
        修改车辆的状态
         */
        if(logCarList!=null&&logCarList.size()>0) {
            LogCar logCar = logCarList.get(0);
            //设置为2--->工作中
            logCar.setLogCarStatus(2);
            logCarMapper.updateByPrimaryKey(logCar);
        }

        //修改出库单的状态，变为1为已经配送
        Example em=new Example(LogGoodsOutIn.class);
        Example.Criteria logoutcriteria=em.createCriteria();
//        根据前端传开的id来查找要派送的编号
        logoutcriteria.andEqualTo("logGoodsOutId",logGoodsOutId);
      List<LogGoodsOutIn> logGoodsOuts= logGoodsOutMapper.selectByExample(em);
      //拿到要派送的订单号
        if (logGoodsOuts!=null&&logGoodsOuts.size()>0) {
            LogGoodsOutIn logorders = logGoodsOuts.get(0);
            //变为2-->已经配送
            logorders.setStatus(2);
            //修改订单状态为已配送
            logGoodsOutMapper.updateByPrimaryKey(logorders);
        }
      /*
      往车辆运输表增添一条记录
       */
        LogTrans logTrans=new LogTrans();
        logTrans.setLogEmployeeName(name);
        logTrans.setLogCarId(logCarId);
        logTrans.setLogGoodsOutId(logGoodsOutId);
        logTrans.setLogTranTime(new Date());
        logTrans.setLogState("运输中");
        logTransMapper.insert(logTrans);

        return  new ResultVo(ResStatus.OK,"派单成功",null);



    }
/*
 查看所有历史记录
  status=1,2，5
  */
    @Override
    public ResultVo finddispatchGoods(int pageNum,int pageSize) {
        List<LogDistachedOrderVO> lists=logGoodsOutMapper.selectDistachOrder();
        if(lists.size()==0) return new ResultVo(ResStatus.NO,"暂时没有派送的订单",null);
      else {
            PageHelper.startPage(pageNum,pageSize);//关键
            List<LogDistachedOrderVO> list =logGoodsOutMapper.selectDistachOrder();
            PageInfo data= new PageInfo(list);
            return new ResultVo(ResStatus.OK, "success", data);
        }
    }

    /*
    出库操作
     */
    @Override
    public ResultVo outGoods(int logGoodsOutId) {
        //根据Id查找相应的商品信息
        List<LogGoodsOutDetailVO> list=logGoodsOutMapper.selectOutGoodsDetailByID(logGoodsOutId);
        //不存在返回错误信息
        if(list.size()==0) return  new ResultVo(ResStatus.NO,"出库库品不存在",null);
        LogGoodsOutDetailVO detailVO =list.get(0);
        /*
        计算商品相关的容量等信息
         */
        Double goodsTotalVolume=detailVO.getLogGoodsTotalVolume();

        //查询该货品对应的仓库记录
        String depotName=detailVO.getLogGoodsOutDepot();
        Example example=new Example(LogDepot.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("logDepotName",depotName);
        List<LogDepot> depotList=logDepotMapper.selectByExample(example);
        if(depotList.size()==0) return new ResultVo(ResStatus.NO,"对应仓库不存在",depotList);
        //拿到对应的仓库
        LogDepot logDepot=depotList.get(0);
        //拿到仓库的剩余容量
        Double totalVolume=logDepot.getLogDepotRemainder();
        //出库后对应仓库容量等于之前容量加上库品总容量
        Double remainderVolume=totalVolume+goodsTotalVolume;
        logDepot.setLogDepotRemainder(remainderVolume);
        logDepotMapper.updateByPrimaryKey(logDepot);

        /*
        对应的出库表状态修改未由0修改为1，表示未发货
         */
        Example goodsoutexample=new Example(LogGoodsOutIn.class);
        Example.Criteria goodoutcriteria=goodsoutexample.createCriteria();
       goodoutcriteria.andEqualTo("status",0);
        goodoutcriteria.andEqualTo("logGoodsOutId",logGoodsOutId);
        List<LogGoodsOutIn> goodlists=logGoodsOutMapper.selectByExample(goodsoutexample);
        if(goodlists.size()==0) if(depotList.size()==0) return new ResultVo(ResStatus.NO,"对应库品不存在",goodlists);
        LogGoodsOutIn logGoodsOutIn=goodlists.get(0);
        //状态设置为1，表示已经出库待派送
        logGoodsOutIn.setStatus(1);
        logGoodsOutMapper.updateByPrimaryKey(logGoodsOutIn);

        return new ResultVo(ResStatus.OK,"success",null);
    }
/*
查找所有已经出库待配送的库品，即status->1
根据仓库
 */
    @Override
    public ResultVo findAllOutGoods(int pageNum, int pageSize,String depot) {
        PageHelper.startPage(pageNum,pageSize);//关键
        Example example=new Example(LogGoodsOutIn.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("status",1);
        criteria.andEqualTo("logGoodsOutDepot",depot);
        List<LogGoodsOutIn> lists=logGoodsOutMapper.selectByExample(example);
        if(lists.size()==0) return new ResultVo(ResStatus.OK,"没有出库的商品",lists);

        PageInfo data= new PageInfo(lists);
        return new ResultVo(ResStatus.OK,"success",data);

    }

    @Override
    public ResultVo transCompleted(int logGoodsOutId) {
/*
得到对应商品订单,并设置对应status- 5已到达
 */
        Example goodsoutexample=new Example(LogGoodsOutIn.class);
        Example.Criteria goodoutcriteria=goodsoutexample.createCriteria();
        //2-->对应运输中
        goodoutcriteria.andEqualTo("status",2);
        goodoutcriteria.andEqualTo("logGoodsOutId",logGoodsOutId);
        List<LogGoodsOutIn> goodlists=logGoodsOutMapper.selectByExample(goodsoutexample);
        if(goodlists.size()==0) return  new ResultVo(ResStatus.OK,"库品获取异常",null);
        LogGoodsOutIn logGoodsOutIn=goodlists.get(0);
        logGoodsOutIn.setStatus(5);//设置为5表示已到达
        logGoodsOutMapper.updateByPrimaryKey(logGoodsOutIn);
        /*
        操作运输表修改信息
         */
         Example TransExample=new Example(LogTrans.class);
         Example.Criteria TransCriteria=TransExample.createCriteria();
         TransCriteria.andEqualTo("logGoodsOutId",logGoodsOutId);
         //获取对应的运输表信息
         List<LogTrans> Tranlists=logTransMapper.selectByExample(TransExample);
         if(Tranlists.size()==0) return new ResultVo(ResStatus.NO,"运输异常错误",Tranlists);
         LogTrans logTrans=Tranlists.get(0);
         logTrans.setLogTranTime(new Date());
         //运输表状态设置为已到达
         logTrans.setLogState("已到达");
         logTransMapper.updateByPrimaryKey(logTrans);
         //拿到对应司机名字,和车辆Id
         String EmployeeName=logTrans.getLogEmployeeName();
         int carId =logTrans.getLogCarId();
         //对司机状态进行修改，变成空闲中
        Example EmployeeExample=new Example(LogTrans.class);
        Example.Criteria EmployeeCriteria=EmployeeExample.createCriteria();
        EmployeeCriteria.andEqualTo("logEmployeeName",EmployeeName);
        //获取对应的运输表信息
        List<LogEmployeer> Employeelists=logEmployeerMapper.selectByExample(EmployeeExample);
        if(Employeelists.size()==0) return new ResultVo(ResStatus.NO,"司机获取异常错误",Employeelists);
         LogEmployeer logEmployeer=Employeelists.get(0);
         //司机状态设置为0表示空闲
         logEmployeer.setLogEmployeerState(0);
         logEmployeerMapper.updateByPrimaryKey(logEmployeer);
         /*
         修改对应车辆状态
          */
        //对司机状态进行修改，变成空闲中
        Example CarExample=new Example(LogCar.class);
        Example.Criteria CarCriteria=CarExample.createCriteria();
        CarCriteria.andEqualTo("logCarId",carId);
        //获取对应的运输表信息
        List<LogCar> Carlists=logCarMapper.selectByExample(CarExample);
        if(Carlists.size()==0) return new ResultVo(ResStatus.NO,"车辆获取异常",Carlists);
        //获取车辆
        LogCar car =Carlists.get(0);
//        状态设置为1表示可用
        car.setLogCarStatus(1);
        logCarMapper.updateByPrimaryKey(car);


        return new ResultVo(ResStatus.OK,"操作成功",null);
    }
/*
查找所有未出库的商品
根据仓库
 */
    @Override
    public ResultVo findAllNorOutGoods(int pageNum, int pageSize,String depot) {
        PageHelper.startPage(pageNum,pageSize);//关键
        Example example=new Example(LogGoodsOutIn.class);
        Example.Criteria criteria=example.createCriteria();
        //0表示未出库
        criteria.andEqualTo("status",0);
        criteria.andEqualTo("logGoodsOutDepot",depot);
        List<LogGoodsOutIn> lists=logGoodsOutMapper.selectByExample(example);
        if(lists.size()==0) return new ResultVo(ResStatus.NO,"没有数据",lists);

        PageInfo data= new PageInfo(lists);
        return new ResultVo(ResStatus.OK,"success",data);

    }
//管理员查找所有未出库商品
    @Override
    public ResultVo AdminfindAllNorOutGoods(int pageNum, int pageSize) {
        Example example=new Example(LogGoodsOutIn.class);
        Example.Criteria criteria=example.createCriteria();
        PageHelper.startPage(pageNum,pageSize);//关键
        //0表示未出库
        criteria.andEqualTo("status",0);
        List<LogGoodsOutIn> lists=logGoodsOutMapper.selectByExample(example);

        PageInfo data= new PageInfo(lists);
        return new ResultVo(ResStatus.OK,"success",data);

    }
//Admin查看所有以及出库的订单
    @Override
    public ResultVo AdminfindAllOutGoods(int pageNum, int pageSize) {
        Example example=new Example(LogGoodsOutIn.class);
        Example.Criteria criteria=example.createCriteria();
        PageHelper.startPage(pageNum,pageSize);//关键
        //0表示未出库
        criteria.andEqualTo("status",1);
        List<LogGoodsOutIn> lists=logGoodsOutMapper.selectByExample(example);

        PageInfo data= new PageInfo(lists);
        return new ResultVo(ResStatus.OK,"success",data);
    }

    @Override
    public ResultVo findAllGoodsByDepot(int pageNum, int pageSize, String depot) {
        Example example=new Example(LogGoodsOutIn.class);
        Example.Criteria criteria=example.createCriteria();
        PageHelper.startPage(pageNum,pageSize);//关键
        criteria.andEqualTo("logGoodsOutDepot",depot);
        List<LogGoodsOutIn> lists=logGoodsOutMapper.selectByExample(example);

        PageInfo data= new PageInfo(lists);
        return new ResultVo(ResStatus.OK,"success",data);
    }
}
